package com.skyworth.web.admin.role.controller;

import com.skyworth.web.admin.role.domain.PermissionModel;
import com.skyworth.web.admin.role.domain.RoleModel;
import com.skyworth.web.admin.role.form.RoleForm;
import com.skyworth.web.admin.role.service.PermissionService;
import com.skyworth.web.admin.role.service.RoleService;
import com.skyworth.web.admin.user.domain.UserModel;
import com.skyworth.web.common.Constants;
import com.skyworth.web.common.controller.BaseController;
import com.skyworth.web.common.form.MenuCss;
import com.skyworth.web.common.form.Paginator;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by skyworth on 2016/9/22.
 */
@Controller
@RequiresRoles(value={Constants.ROLE_ROLE})
@RequestMapping(value="/admin/role")
public class RoleController extends BaseController {

	Logger LOG = LoggerFactory.getLogger(RoleController.class);

	@Autowired
	private RoleService roleService;

	@Autowired
	private PermissionService permissionService;

	@Autowired
	private MenuCss menuCss;


	@RequiresRoles(value={Constants.ROLE_ROLE})
	@RequestMapping("")
	public String index(Paginator paginator, Model model) {
		UserModel userModel = currentUser();
		long store_id = 0 ;
		if(userModel != null){
			store_id = userModel.storeId;
		}

		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouRole = "hover";
		model.addAttribute("menuCss", menuCss);
		model.addAttribute("user", userModel);
		Page<RoleModel> page = roleService.pageRole(store_id,paginator.pageRequest());
		paginator.feedToModel(page, model);
		return "admin/role/roles";
	}

	@RequestMapping(value = {"new"})
	public String newAction(Model model) {

		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouRole = "hover";
		model.addAttribute("menuCss", menuCss);
		model.addAttribute("user", currentUser());
		List<PermissionModel> permissionModelList =  permissionService.listPermission();
		model.addAttribute("permissions", permissionModelList);

		return "admin/role/role_new";
	}


	@RequestMapping("{id}")
	public String edit(@PathVariable("id") long id, Model model) {

		menuCss = new MenuCss();
		menuCss.currentFanfou = "show";
		menuCss.currentFanfouRole = "hover";

		model.addAttribute("menuCss", menuCss);

		RoleModel roleModel = roleService.findRole(id);
		List<PermissionModel> permissionModelList =  permissionService.listPermission();
		model.addAttribute("permissions", permissionModelList);

		model.addAttribute("item", roleModel);
		model.addAttribute("user", currentUser());

		return "admin/role/role_edit";
	}


	@RequestMapping(value="update", method = RequestMethod.PUT)
	@ResponseBody
	public Map<String,Object> update(@RequestBody Map<String, Object> roleMap,
						 Model model) {
		Map<String, Object> map = new HashMap<String, Object>();

		RoleForm roleForm = new RoleForm();
		roleForm.id = Integer.parseInt(roleMap.get("id").toString());
		roleForm.name = roleMap.get("name").toString();
		roleForm.permissionIds = roleMap.get("permissionIds").toString();

		UserModel userModel = currentUser();
		long store_id = 0 ;
		if(userModel != null){
			store_id = userModel.storeId;
		}else {
			map.put("valid", false);
			map.put("errorMsg", "请先登陆");
			return map;
		}

		if(roleService.existRole(roleForm.getName(), store_id)){
			map.put("valid", false);
			map.put("errorMsg", roleForm.getName()+"已存在");
			return map;
		}

		RoleModel roleModel = roleService.updateRole(roleForm);

		model.addAttribute("item", roleModel);
		model.addAttribute("user", currentUser());
		map.put("valid", true);
		map.put("url", "/admin/role");
		return map;
	}

	@RequestMapping(value="{id}",method = RequestMethod.DELETE)
	@ResponseBody
	public Map<String,Object> delete(@PathVariable("id") long id ) {
		RoleModel roleModel= roleService.findRole(id);

		Map<String, Object> rsp = responseMap();
		if (roleModel != null) {
			roleService.delRole(id);
		}else{
			rsp.put("success", false);
			rsp.put("message", "dishware doesn't exist");
		}
		return rsp;
	}



	@RequestMapping(value = {"create"}, method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object>  create(@RequestBody Map<String, Object> roleMap,
						 Model model) {
		Map<String, Object> map = new HashMap<String, Object>();

		RoleForm roleForm = new RoleForm();
		roleForm.name = roleMap.get("name").toString();
		roleForm.permissionIds = roleMap.get("permissionIds").toString();

		long store_id = 0;
		UserModel userModel = currentUser();
		if(null != userModel){
			store_id = userModel.storeId;
		}else {
			map.put("valid", false);
			map.put("errorMsg", "请先登录");
			return map;
		}

		if (roleService.existRole(roleForm.getName(), store_id)) {
			map.put("valid", false);
			map.put("errorMsg", "角色已经存在");
			return map;
		}
		roleForm.setStoreId(store_id);
		roleForm.modifyFlag = 1;
		RoleModel roleModel = roleService.addRole(roleForm);

		model.addAttribute("user", userModel);
		if (roleModel.getId() > 0) {
			map.put("valid", true);
			map.put("url", "/admin/role");
		} else {
			map.put("valid", false);
			map.put("errorMsg", "添加异常");
		}
		return map;
	}
}
